package com.example.springbooteasycode.utils.word.bad;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;

public class GenerateStyledWordDouBaoNice {
    public static void main(String[] args) {
        try (XWPFDocument document = new XWPFDocument()) {
            // 设置标题样式
            XWPFParagraph titleParagraph = document.createParagraph();
            titleParagraph.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun titleRun = titleParagraph.createRun();
            titleRun.setText("第__届业主委员会委员候选人报名表");
            titleRun.setFontSize(18);
            titleRun.setBold(true);

            // 创建表格
            XWPFTable table = document.createTable(6, 6);
            setTableStyle(table);

            // 填充表头
            setCellText(table.getRow(0).getCell(0), "姓 名");
            setCellText(table.getRow(0).getCell(1), "测试1");
            setCellText(table.getRow(0).getCell(2), "性 别");
            setCellText(table.getRow(0).getCell(3), "男");
            setCellText(table.getRow(0).getCell(4), "出生\n年月");
            setCellText(table.getRow(0).getCell(5), "1981年2月");

            setCellText(table.getRow(1).getCell(0), "职 业");
            setCellText(table.getRow(1).getCell(1), "公务员");
            setCellText(table.getRow(1).getCell(2), "文化\n程度");
            setCellText(table.getRow(1).getCell(3), "硕士");
            setCellText(table.getRow(1).getCell(4), "政治\n面貌");
            setCellText(table.getRow(1).getCell(5), "中共党员");

            setCellText(table.getRow(2).getCell(0), "专有部分座落");
            setCellText(table.getRow(2).getCell(1), "1-1-101");
            setCellText(table.getRow(2).getCell(2), "专有部分面积");
            setCellText(table.getRow(2).getCell(3), "98.00 ㎡");

            // 报名理由段落
            XWPFParagraph reasonParagraph = document.createParagraph();
            reasonParagraph.setSpacingBefore(10);
            reasonParagraph.setSpacingAfter(10);
            XWPFRun reasonRun = reasonParagraph.createRun();
            reasonRun.setText("报名理由:");

            XWPFParagraph longReasonParagraph = document.createParagraph();
            longReasonParagraph.setIndentationFirstLine(400);
            XWPFRun longReasonRun = longReasonParagraph.createRun();
            longReasonRun.setText("报名理由报名理由报名理由报名理由报名理由报名理由报名理由报名理由报名理由报名理由");

            // 签名段落
            XWPFParagraph signatureParagraph = document.createParagraph();
            signatureParagraph.setAlignment(ParagraphAlignment.RIGHT);
            signatureParagraph.setSpacingBefore(20);
            XWPFRun signatureRun = signatureParagraph.createRun();
            signatureRun.setText("签 名:");

            // 交表时间段落
            XWPFParagraph timeParagraph = document.createParagraph();
            timeParagraph.setAlignment(ParagraphAlignment.RIGHT);
            XWPFRun timeRun = timeParagraph.createRun();
            timeRun.setText("交表时间 2025年2月19日15时");

            // 保存文件
            try (FileOutputStream out = new FileOutputStream("/Users/boykchen/Desktop/styled_candidate_application_form.docx")) {
                document.write(out);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void setCellText(XWPFTableCell cell, String text) {
        XWPFParagraph paragraph = cell.getParagraphs().isEmpty()? cell.addParagraph() : cell.getParagraphArray(0);
        XWPFRun run = paragraph.getRuns().isEmpty()? paragraph.createRun() : paragraph.getRuns().get(0);
        run.setText(text);
    }

    private static void setTableStyle(XWPFTable table) {
        CTTblPr tblPr = table.getCTTbl().getTblPr();
        if (tblPr == null) {
            tblPr = table.getCTTbl().addNewTblPr();
        }

        // 设置表格宽度为自动
        CTTblWidth width = tblPr.isSetTblW()? tblPr.getTblW() : tblPr.addNewTblW();
        width.setType(STTblWidth.AUTO);

        // 设置表格边框
        CTTblBorders borders = tblPr.isSetTblBorders()? tblPr.getTblBorders() : tblPr.addNewTblBorders();
        setBorder(borders.addNewTop(), STBorder.SINGLE, 1, 0, "000000");
        setBorder(borders.addNewBottom(), STBorder.SINGLE, 1, 0, "000000");
        setBorder(borders.addNewLeft(), STBorder.SINGLE, 1, 0, "000000");
        setBorder(borders.addNewRight(), STBorder.SINGLE, 1, 0, "000000");
        setBorder(borders.addNewInsideH(), STBorder.SINGLE, 1, 0, "000000");
        setBorder(borders.addNewInsideV(), STBorder.SINGLE, 1, 0, "000000");

        // 设置单元格内文本垂直居中
        for (XWPFTableRow row : table.getRows()) {
            for (XWPFTableCell cell : row.getTableCells()) {
                CTTcPr tcPr = cell.getCTTc().isSetTcPr()? cell.getCTTc().getTcPr() : cell.getCTTc().addNewTcPr();
                CTVerticalJc vAlign = tcPr.isSetVAlign()? tcPr.getVAlign() : tcPr.addNewVAlign();
                vAlign.setVal(STVerticalJc.CENTER);
            }
        }
    }

    private static void setBorder(CTBorder border, STBorder.Enum type, int size, int space, String color) {
        border.setVal(type);
        border.setSz(BigInteger.valueOf(size));
        border.setSpace(BigInteger.valueOf(space));
        border.setColor(color);
    }

}
